package cn.spike.org.service.impl;

import cn.spike.basic.service.impl.BaseServiceImpl;
import cn.spike.basic.util.Md5Utils;
import cn.spike.basic.util.PageList;
import cn.spike.basic.util.StrUtils;
import cn.spike.org.domain.Employee;
import cn.spike.org.mapper.EmployeeMapper;
import cn.spike.org.query.EmployeeQuery;
import cn.spike.org.service.EmployeeService;
import cn.spike.user.domain.Logininfo;
import cn.spike.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author:sea
 * @create:2023-01-09 18:15
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Transactional
    @Override
    public void add(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //先添加logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);
        logininfoMapper.add(logininfo);//获取到自增ID

        //添加user的数据
        employee.setLogininfo_id(logininfo.getId());
        super.add(employee);

        //添加员工角色中间表信息
        if (employee.getRole_id() != null) {
            employeeMapper.addEmployeeRole(employee);
        }
    }

    @Transactional
    @Override
    public void delete(Long id) {
        //【角色】删除角色信息
        employeeMapper.delEmployeeRoleByEmployeeId(id);
        //先查询登陆信息id
        Employee employee = employeeMapper.loadById(id);
        //删除logininfo
        logininfoMapper.delete(employee.getLogininfo_id());

        //删除employee
        super.delete(id);
    }

    @Transactional
    @Override
    public void update(Employee employee) {
        String oldPassword = employee.getPassword();
        //查以前的老数据：employee里面大的数据来自于前端  -就不会有logininfo_id,shop_id
        Employee oldEmployee = employeeMapper.loadById(employee.getId());
        //如果前端传过来的密码和数据库中加密加盐后的密码不一致
        //说明更改了密码
        if (!oldPassword.equals(oldEmployee.getPassword())) {
            //处理盐值和密码
            String salt = StrUtils.getComplexRandomString(32);
            String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
            employee.setSalt(salt);
            employee.setPassword(md5Pwd);
        }
        //修改user信息
        System.out.println(employee.getLogininfo_id());
        System.out.println(employee.getShop_id());
        employee.setLogininfo_id(oldEmployee.getLogininfo_id());
        employee.setShop_id(oldEmployee.getShop_id());
        super.update(employee);

        //修改登陆信息
        Logininfo oldLogininfo = logininfoMapper.loadById(oldEmployee.getLogininfo_id());
        BeanUtils.copyProperties(employee, oldLogininfo);
        //如果员工的状态修改为了禁用
        if (employee.getState() == 0) {
            //登录信息的状态也需要同步
            oldLogininfo.setDisable(false);
        } else {
            oldLogininfo.setDisable(true);
        }
        oldLogininfo.setId(oldEmployee.getLogininfo_id());
        logininfoMapper.update(oldLogininfo);

        //先删除角色信息在添加 = 修改
        employeeMapper.delEmployeeRoleByEmployeeId(employee.getId());
        employeeMapper.addEmployeeRole(employee);


    }


    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        List<Long> LogininfoIds = employeeMapper.patchSelectLogininfoId(ids);
        logininfoMapper.patchDelete(LogininfoIds);
        employeeMapper.patchDelEmpRoleByEmpId(ids);
        super.patchDelete(ids);
    }

    @Override
    public List<Employee> queryByShopId(Long shopId) {
        if(shopId!=null){
            return employeeMapper.queryByShopId(shopId);
        }
        return employeeMapper.queryByShopId(shopId);
    }
}
